package org.wekit.engine.pvm.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.wekit.engine.pvm.ExecutionListener;
import org.wekit.engine.pvm.PvmActivity;
import org.wekit.engine.pvm.PvmException;
import org.wekit.engine.pvm.PvmScope;

/**
 * 
 * @author HuangWeili
 *
 */
public class Scope extends ProcessElement implements PvmScope {


	private static final long serialVersionUID = -6434591861933355848L;

	/**
	 * 获取范围内的流程节点
	 */
	protected List<Activity> activities=new ArrayList<Activity>();
	
	/**
	 * 获取名称节点对应表
	 */
	protected Map<String, Activity> namedActivities=new HashMap<String,Activity>();
	
	/**
	 * 注册时间今天监听对象
	 */
	protected Map<String ,List<ExecutionListener>> executionListeners=new HashMap<String, List<ExecutionListener>>();
	
	
	
	public Scope(String id, ProcessDefinition processDefinition) {
		super(id, processDefinition);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<? extends PvmActivity> getActivities() {
		return this.activities;
	}

	/**
	 * 遍历所有的节点查找目标数据，包含多层的嵌套
	 */
	@Override
	public Activity findActivity(String activityId) {
		Activity localActivity=namedActivities.get(activityId);
		if(null!=localActivity)
		{
			return localActivity;
		}
		for(Activity activity: activities)
		{
			Activity nestedActivity=activity.findActivity(activityId);
			if(nestedActivity!=null)
			  return nestedActivity;
		}
		return null;
	}
	
	public Activity createActivity(){
	   return createActivity(null);
	}
	
	
	/**
	 * 创建子活动点
	 * @param activityId
	 * @return
	 */
	public Activity createActivity(String activityId){
		Activity activity=new Activity(activityId, this.processDefinition);
		if(null!=activityId)
		{
			if(this.processDefinition.findActivity(activityId)!=null)
			{
				throw new PvmException("duplicate activity id "+activityId);
			}
			namedActivities.put(activityId, activity);
		}
	    activity.setParent(this);
		activities.add(activity);
		return activity;
	}
	
	public boolean contains(Activity activity)
	{
		if(namedActivities.containsKey(activity.getId()))
		{
			return true;
		}
		for(Activity nestedActivity:activities)
		{
			if(nestedActivity.contains(activity))
				return true;
		}
		return false;
	}
	
	
	/**
	 * 添加事件监听
	 * @param eventName
	 * @param executionListener
	 */
	public void addExecutionListener(String eventName,ExecutionListener executionListener){
		this.addExecutionListener(eventName, executionListener, -1);
	}
	
	
	/**
	 * 添加时间监听
	 * @param eventName  事件名称
	 * @param executionListener 监听者
	 * @param index  序列
	 */
	public void addExecutionListener(String eventName,ExecutionListener executionListener,int index)
	{
		List<ExecutionListener> listeners=executionListeners.get(eventName);
		if(null==listeners)
		{
			listeners=new ArrayList<ExecutionListener>();
			executionListeners.put(eventName, listeners);
		}
		if(index<0)
		{
		  listeners.add(executionListener);
		}else
		{
			listeners.add(index,executionListener);
		}
	}
	
	
	
	/**
	 * 通过事件名称获取事件监听
	 * @param eventName
	 * @return
	 */
	public List<ExecutionListener> getExecutionListeners(String eventName)
	{
		List<ExecutionListener> executionListenerList=getExecutionListeners().get(eventName);
		if(executionListenerList!=null)
		{
			return executionListenerList;
		}
		
		return new ArrayList<ExecutionListener>();
	}
	
	//返回所有监听事件
	public Map<String,List<ExecutionListener>> getExecutionListeners(){
		return executionListeners;
	}
	
}
